package org.linlinjava.litemall.admin.mybatis;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Properties;
import java.util.List;

//@Component
@Intercepts({@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class VendorPlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
//        Object[] args = invocation.getArgs();
//        MappedStatement ms = (MappedStatement) args[0];
//        Object parameter = args[1];
//        RowBounds rowBounds = (RowBounds) args[2];
//        ResultHandler resultHandler = (ResultHandler) args[3];
//        Executor executor = (Executor) invocation.getTarget();
//        CacheKey cacheKey;
//        BoundSql boundSql;
//
//        try {
//            MetaObject mo = SystemMetaObject.forObject(parameter);
//            if(mo.hasGetter("oredCriteria")) {
//                Subject currentUser = SecurityUtils.getSubject();
//                LitemallAdmin vendor = (LitemallAdmin) currentUser.getPrincipal();
//
//                List<Object> list = (List<Object>) mo.getValue("oredCriteria");
//                if(null != list) {
//                    for (Object obj : list) {
//                        Method method = obj.getClass().getDeclaredMethod("andVendorIdEqualTo", Integer.class);
//                        method.invoke(obj, vendor.getId());
//                    }
//                }
//            }
//        } catch (Exception e) {
//
//        }
//
//        //由于逻辑关系，只会进入一次
//        if(args.length == 4){
//            //4 个参数时
//            boundSql = ms.getBoundSql(parameter);
//            cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
//        } else {
//            //6 个参数时
//            cacheKey = (CacheKey) args[4];
//            boundSql = ms.getBoundSql(parameter);
//        }
//
//        List resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
//        return resultList;
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
